package com.developer.platform.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.developer.platform.domain.DictData;
import com.developer.platform.domain.DictType;
import com.developer.platform.domain.repository.IDictRepository;
import com.developer.platform.infrastructure.data.mapper.DictDataMapper;
import com.developer.platform.infrastructure.data.mapper.DictTypeMapper;
import org.springframework.stereotype.Service;

/**
 * @author ：qiaoliang
 * @date ：2020-10-28
 */
@Service
public class DictRepository implements IDictRepository {

    private final DictDataMapper dictDataMapper;
    private final DictTypeMapper dictTypeMapper;

    public DictRepository(DictDataMapper dictDataMapper,
                          DictTypeMapper dictTypeMapper) {
        this.dictDataMapper = dictDataMapper;
        this.dictTypeMapper = dictTypeMapper;
    }

    @Override
    public void saveType(DictType dictType) {
        dictTypeMapper.insert(dictType);
    }

    @Override
    public void saveData(DictData dictData) {
        dictDataMapper.insert(dictData);
    }

    @Override
    public void updateType(DictType dictType) {
        dictTypeMapper.updateById(dictType);
    }

    @Override
    public void updateData(DictData dictData) {
        dictDataMapper.updateById(dictData);
    }

    @Override
    public void deleteType(String type) {
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictType::getType, type);
        dictTypeMapper.delete(queryWrapper);
        LambdaQueryWrapper<DictData> dataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dataLambdaQueryWrapper.eq(DictData::getType, type);
        dictDataMapper.delete(dataLambdaQueryWrapper);
    }

    @Override
    public void deleteData(Long id) {
        dictDataMapper.deleteById(id);
    }
}
